package com.txm.qqserver.service;

import com.txm.qqcommon.*;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 每登录一个用户，就创建一个accept管道连接线程，用于接收用户指令
* */
public class ServerConnectServerThread extends Thread {
    ServerSocket serverSocket;
    Socket accept;
    public User user;

    public boolean isLive = true;

    public String msgContent = "";

    public String groupId = "";

    public ServerConnectServerThread(ServerSocket serverSocket, User user) {
        this.serverSocket = serverSocket;
        this.user = user;
    }

    public ServerConnectServerThread(Socket accept, User user) {
        this.accept = accept;
        this.user = user;
    }

    @Override
    public void run() {
        try {
            while (isLive) {
                System.out.println("等待客服端" + user.getUsername() + "用户的指令：");
                ObjectInputStream objectInputStream = new ObjectInputStream(accept.getInputStream());
                Response res = (Response) objectInputStream.readObject();
                System.out.println(this.getName() + "客户端传来的数据：" + res);
                Message msg = (Message) res.getObject();

                if (msg.getMsgType().equals(MessageType.MESSAGE_SYSTEM_EXIT)) {
                    User u = (User) msg.getObject();
                    ServerConnectServerThread thread = (ServerConnectServerThread) ManageClientThreads.getMcts().get(u.getUserId());
                    if (this == thread) {
                        ManageClientThreads.removeMctsByKey(u.getUserId());
                        break;
                    }
                }
                if (msg.getMsgType().equals(MessageType.MESSAGE_SENDER_FILE_ING)) {
                    try {
                        File_ object = (File_) msg.getObject();
                        if (object.getStatus() == 0) {
                            HandleTransferFile handleTransferFile = new HandleTransferFile(res);
                            handleTransferFile.start();
                            handleTransferFile.join();
                        }
                    } catch (Exception e) {
                        System.out.println("Exception" + e);
                    }
                    continue;
                }

                UserServerService.getOnlineUsers(res, this);
                PersonChat.checkUserOnline(res, this);
                HandleTransferFile.handleFileMsg(res, this);
                GroupChat.handleMethods(res);
            }
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(accept.getOutputStream());
            Message message = new Message();
            message.setCurrentLocalDate();
            message.setGetter(user.getUserId());
            message.setMsgType(MessageType.MESSAGE_SYSTEM_EXIT);
            message.setContent(msgContent);
            message.setObject(user);
            objectOutputStream.writeObject(message);
            System.out.println(this.getName() + "退出了服务。");
            System.out.println(ManageClientThreads.getMcts());
        } catch (Exception e) {
            System.out.println("ServerConnectServerThread Exception：" + e);
//            e.printStackTrace();
            if (e.toString().equals("java.net.SocketException: Connection reset")) {
                ManageClientThreads.removeMctsByKey(user.getUserId());
            }
        }
    }
}
